package Leetcode;

import java.util.Arrays;

public class Test27 {

    /**
     * 输入：nums = [0,1,2,2,3,0,4,2], val = 2
     * 输出：5, nums = [0,1,4,0,3,_,_,_]
     * 目标：去除数组中等于val的数字并用空值代替
     * @param nums
     * @param val
     * @return 该方法返回的值是数值不为空的数量
     */
    public static int removeElement(int[] nums, int val){

        int k = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i]!=val){
                nums[k] = nums[i];
                k++;
            }
        }
        System.out.println(Arrays.toString(nums));
        return k;

    }

    public static void main(String[] args) {
        int[] nums = {0,1,2,2,3,0,4,2};
        int val = 2;

        int newLength = removeElement(nums, val);

        // 输出新长度和移除元素后的数组内容
        System.out.println("新长度: " + newLength);
        for (int i = 0; i < newLength; i++) {
            System.out.print(nums[i] + " ");
        }
    }

}
